package com.tt.cline.utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author tiantao
 * @date 2022/2/23 15:01
 */
public class GetHttpURLConnection {
    public static java.net.HttpURLConnection buildRequestBaseInfo(String requestUrl, String paramKey, String paramVlaue) throws IOException {
        URL url = new URL(requestUrl);
        URLConnection urlConnection = url.openConnection();
        java.net.HttpURLConnection httpURLConnection = (java.net.HttpURLConnection) urlConnection;

        // true -- will setting parameters
        httpURLConnection.setDoOutput(true);
        // true--will allow read in from
        httpURLConnection.setDoInput(true);
        // will not use caches
        httpURLConnection.setUseCaches(false);
        // setting serialized
        httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
        // default is GET
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Charsert", "UTF-8");
        // 1 min
        httpURLConnection.setConnectTimeout(60000);
        // 1 min
        httpURLConnection.setReadTimeout(60000);

        httpURLConnection.addRequestProperty(paramKey, paramVlaue);

        // connect to server (tcp)
        httpURLConnection.connect();
        return httpURLConnection;
    }
}
